#!/bin/bash

# This script needs to be run with root rights.
if [ $UID -ne 0 ]; then
    sudo $0
    exit 0
fi

function printNotSupportedMessageAndExit() {
    echo
    echo "Currently this script only works for distributions supporting apt-get and yum."
    echo "Please add support for your distribution: http://webkit.org/b/110693"
    echo
    exit 1
}

function checkInstaller {
    # apt-get - Debian based distributions
    apt-get --version &> /dev/null
    if [ $? -eq 0 ]; then
        installDependenciesWithApt
        exit 0
    fi

    # yum - Fedora
    yum --version &> /dev/null
    if [ $? -eq 0 ]; then
        installDependenciesWithYum
        exit 0
    fi

    # pacman - Arch Linux
    # pacman --version and pacman --help both return non-0
    pacman -Ss &> /dev/null
    if [ $? -eq 0 ]; then
        installDependenciesWithPacman
        exit 0
    fi

    printNotSupportedMessageAndExit
}

function installDependenciesWithApt {
    # These are dependencies necessary for building WebKitGTK+.
    apt-get install \
        autoconf \
        automake \
        autopoint \
        autotools-dev \
        bison \
        cmake \
        flex \
        gawk \
        gnome-common \
        gperf \
        gtk-doc-tools \
        intltool \
        itstool \
        libatk1.0-dev \
        libenchant-dev \
        libfaad-dev \
        libgeoclue-dev \
        libgirepository1.0-dev \
        libgl1-mesa-dev \
        libgl1-mesa-glx \
        libgnutls28-dev \
        libgtk2.0-dev \
        libgtk-3-dev \
        libgudev-1.0-dev \
        libharfbuzz-dev \
        libicu-dev \
        libjpeg8-dev \
        libmpg123-dev \
        libopus-dev \
        libpango1.0-dev \
        libpng12-dev \
        libpulse-dev \
        librsvg2-dev \
        libsecret-1-dev \
        libsoup2.4-dev \
        libsqlite3-dev \
        libtheora-dev \
        libtool \
        libvorbis-dev \
        libwebp-dev \
        libxcomposite-dev \
        libxslt1-dev \
        libxt-dev \
        libxtst-dev \
        ruby \
        xfonts-utils

    # These are dependencies necessary for running tests.
    apt-get install \
        apache2 \
        curl \
        dbus-x11 \
        libapache2-mod-bw \
        libapache2-mod-php5 \
        libgpg-error-dev \
        pulseaudio-utils \
        python-gi \
        ruby \
        ruby-json \
        ruby-highline \
        xvfb

    # These are dependencies necessary for building the jhbuild.
    apt-get install \
        git \
        gobject-introspection \
        icon-naming-utils \
        libcroco3-dev \
        libegl1-mesa-dev \
        libgcrypt11-dev \
        libgpg-error-dev \
        libp11-kit-dev \
        libpciaccess-dev \
        libtiff5-dev \
        libxcb-xfixes0-dev \
        libxfont-dev \
        libxkbfile-dev \
        llvm \
        llvm-dev \
        python-dev \
        ragel \
        x11proto-bigreqs-dev \
        x11proto-composite-dev \
        x11proto-gl-dev \
        x11proto-input-dev \
        x11proto-randr-dev \
        x11proto-resource-dev \
        x11proto-scrnsaver-dev \
        x11proto-video-dev \
        x11proto-xcmisc-dev \
        x11proto-xf86dri-dev \
        xfonts-utils \
        xtrans-dev \
        xutils-dev

    # These are dependencies necessary for using webkit-patch
    apt-get install \
        git-svn \
        subversion

    # ninja is a faster build system than GNU make, but it doesn't
    # exist on Ubuntu 12.04
    apt-get install ninja-build || true
}

function installDependenciesWithPacman {
    # These are dependencies necessary for building WebKitGTK+.
    packages=" \
        autoconf \
        automake \
        bison \
        cmake \
        file \
        findutils \
        flex \
        gawk \
        gcc \
        gettext \
        gnome-common \
        gperf \
        grep \
        groff \
        gzip \
        libtool \
        m4 \
        make \
        patch \
        pkg-config \
        sed \
        texinfo \
        util-linux \
        which \
        gtk-doc \
        intltool \
        itstool \
        atk \
        enchant \
        faad2 \
        geoclue \
        gobject-introspection \
        mesa \
        mesa-libgl \
        gnutls \
        gtk2 \
        gtk3 \
        libsystemd \
        harfbuzz \
        harfbuzz-icu \
        icu \
        libjpeg-turbo \
        mpg123 \
        opus \
        pango \
        libpng \
        libpulse \
        librsvg \
        libsecret \
        libsoup \
        sqlite \
        libtheora \
        libtool \
        libvorbis \
        libwebp \
        libxcomposite \
        libxslt \
        libxt \
        libxtst \
        ninja \
        ruby \
        xorg-font-utils"

    # These are dependencies necessary for running tests.
    # Note: apache-mod_bw is available in the AUR, but the main repos
    #       could not find ruby-json
    packages="$packages \
        apache \
        curl \
        php-apache \
        libgpg-error \
        pulseaudio \
        python-gobject \
        ruby \
        ruby-highline \
        xorg-server-xvfb"

    # These are dependencies necessary for building the jhbuild.
    # Note: Could not find libegl-mesa
    packages="$packages \
        git \
        gobject-introspection \
        icon-naming-utils \
        libcroco \
        libgcrypt \
        libgpg-error \
        p11-kit \
        libpciaccess \
        libtiff \
        libxfixes \
        libxfont \
        libxkbfile \
        llvm \
        python2 \
        python2-lxml \
        ragel \
        bigreqsproto \
        compositeproto \
        glproto \
        inputproto \
        randrproto \
        resourceproto \
        scrnsaverproto \
        videoproto \
        xcmiscproto \
        xf86driproto \
        xorg-font-utils \
        xorg-util-macros \
        xtrans \
        xorg-utils"

    # These are dependencies necessary for using webkit-patch
    packages="$packages \
        svn"
    pacman -S --needed $packages

    echo "You will also need to follow the instructions on the Arch Wiki to make"
    echo "'python' call python2 in the webkit folder"
    echo "https://wiki.archlinux.org/index.php?python#Dealing_with_version_problem_in_build_scripts"
}

function installDependenciesWithYum {
    # These are dependencies necessary for building WebKitGTK+.
    yum install \
        atk-devel \
        autoconf \
        automake \
        bison \
        cairo-devel \
        cmake \
        enchant-devel \
        flex \
        fontconfig-devel \
        freetype-devel \
        gcc-c++ \
        geoclue-devel \
        gettext-devel \
        gobject-introspection-devel \
        gperf \
        gstreamer1-devel \
        gstreamer1-plugins-base-devel \
        gtk-doc \
        gtk2-devel \
        gtk3-devel \
        harfbuzz-devel \
        libXt-devel \
        libXtst-devel \
        libgudev1-devel \
        libicu-devel \
        libjpeg-turbo-devel \
        libpng-devel \
        libsecret-devel \
        libsoup-devel \
        libwebp-devel \
        libxslt-devel \
        mesa-libGL-devel \
        ninja-build \
        pcre-devel \
        perl-Switch \
        perl-version \
        python-devel \
        ruby \
        sqlite-devel

    # These are dependencies necessary for running tests.
    yum install \
        curl \
        dbus-x11 \
        httpd \
        libgpg-error-devel \
        mod_bw \
        mod_ssl \
        perl-CGI \
        php \
        pulseaudio-utils \
        pygobject3-base \
        ruby \
        rubygem-json \
        rubygem-highline \
        xorg-x11-server-Xvfb

    # These are dependencies necessary for building the jhbuild.
    yum install \
        git \
        gobject-introspection \
        icon-naming-utils \
        itstool \
        libXfont-devel \
        libcroco-devel \
        libgcrypt-devel \
        libgpg-error-devel \
        libp11-devel \
        libpciaccess-devel \
        libtiff-devel \
        libxkbfile-devel \
        llvm \
        llvm-devel \
        mesa-libEGL-devel \
        ragel \
        xorg-x11-font-utils \
        xorg-x11-proto-devel \
        xorg-x11-util-macros \
        xorg-x11-xtrans-devel

    # These are dependencies necessary for using webkit-patch
    yum install \
        git-svn \
        subversion
}

checkInstaller

